<?php

namespace App\Company\Controllers;

use App\Admin\Repositories\Company;
use App\Models\CompanyModel;
use App\Services\Tenancy\Tenancy;
use Dcat\Admin\Form;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Traits\HasUploadedFile;
use Illuminate\Support\Facades\Storage;

class CompanyController extends AdminController
{
    use HasUploadedFile;


    public function index(Content $content){

    }
    /**
     * Edit interface.
     *
     * @param Content $content
     * @return Content
     */
    public function updateInfo(Content $content)
    {
        if (CompanyModel::query()->where('id',Tenancy::getTenantId())->value('is_lock') == CompanyModel::VERIFY_WAIT ) {
            return $content
                ->translation($this->translation())
                ->title('认证信息')
                ->description($this->description()['show'] ?? trans('admin.show'))
                ->body($this->detail(Tenancy::getTenantId()));
        }
        return $content
            ->translation($this->translation())
            ->title($this->title())
            ->description($this->description()['edit'] ?? trans('admin.edit'))
            ->body($this->form()->edit(Tenancy::getTenantId()));
    }

    public function updateInfoPut()
    {
        return parent::update(Tenancy::getTenantId()); // TODO: Change the autogenerated stub
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Company(), function (Form $form) {
            $form->action(admin_url('update-info'));
            $imgUrl = admin_url('image');
            $form->display('mobile', '联系方式');
            $form->radio('cur_type', '类型')->options(CompanyModel::TYPE_LIST)->required()
                ->when(CompanyModel::TYPE_PERSON, function (Form $form) use ($imgUrl) {
                    //个人
                    $form->image('identity_card_front', '身份证人像面')->uniqueName()->autoUpload()->rules('required_if:cur_type,1', [
                        'required_if' => '身份证信息不能为空',
                    ])->removable(false)->saveFullUrl()->url($imgUrl)->autoSave(false);
                    $form->image('identity_card_back', '身份证国徽面')->uniqueName()->autoUpload()->rules('required_if:cur_type,1', [
                        'required_if' => '身份证信息不能为空',
                    ])->removable(false)->saveFullUrl()->url($imgUrl)->autoSave(false);
                })->when(CompanyModel::TYPE_ENTERPRISE, function (Form $form) use ($imgUrl) {
                    $form->text('company_name', '名称')->rules('required_if:cur_type,2', [
                        'required_if' => '名称不能为空',
                    ]);
                    $form->image('business_license_photo')->uniqueName()->autoUpload()->rules('required_if:cur_type,2', [
                        'required_if' => '营业执照不能为空',
                    ])->removable(false)->saveFullUrl()->url($imgUrl)->autoSave(false);
                    $form->image('company_logo')->uniqueName()->autoUpload()->rules('required_if:cur_type,2', [
                        'required_if' => '公司LOGO不能为空',
                    ])->removable(false)->saveFullUrl()->url($imgUrl)->autoSave(false);
                })->default(CompanyModel::TYPE_ENTERPRISE);
            $form->text('contact_person_name')->required();
            $form->image('contact_person_avatar')->uniqueName()->autoUpload()->required()->removable(false)->saveFullUrl()->url($imgUrl)->autoSave(false);
            $form->display('created_at');
            $form->display('updated_at');
            //禁用显示按钮
            $form->disableViewButton();
            $form->disableDeleteButton();
            $form->disableEditingCheck();
            $form->disableCreatingCheck();
            $form->disableViewCheck();
            $form->disableListButton();
            $form->disableResetButton();
            if ($form->model()->cur_type >0){
                $form->disableSubmitButton();
                $form->title('个人信息');

            }
        });
    }

    public function image()
    {
        $disk = Storage::disk();
        // 判断是否是删除文件请求
        if ($this->isDeleteRequest()) {
            // 删除文件并响应
            return $this->deleteFileAndResponse($disk);
        }
        // 获取上传的文件
        $file = $this->file();
        // 获取上传的字段名称

        $dir = 'images';

        $result = $disk->putFile($dir, $file);

        return $result
            ? $this->responseUploaded($disk->url($result), $disk->url($result))
            : $this->responseErrorMessage('文件上传失败');
    }
    protected function detail($id)
    {
        return Show::make($id, new Company(), function (Show $show) {
            $show->field('cur_type','类型')->using(CompanyModel::TYPE_LIST);
            $show->field('mobile','联系方式');
            if ($show->model()->cur_type == CompanyModel::TYPE_PERSON){
                $show->field('identity_card_front')->image();
                $show->field('identity_card_back')->image();
            }else{
                $show->field('company_name');
                $show->field('business_license_photo')->image();
                $show->field('company_logo')->image();

            }
            $show->field('contact_person_avatar')->image();
            $show->field('contact_person_name');
//            $show->field('is_lock');
//            $show->field('password');
            $show->disableListButton();
            $show->disableDeleteButton();
            $show->disableListButton();
            $show->disableEditButton();
        });
    }

}
